<?php
namespace Api\Controller;
use Common\Controller\ApibaseController;
use Common\Service\WxpayService;
use Common\Service\AlipayService;

/**
 * 二维码扫一扫
 * @package Api\Controller
 */
class QrcodeController extends ApibaseController {
    

    protected $business_model,$scanPay_model;

    public function _initialize() {
        parent::_initialize();
        $this->checkLogin();

        $this->business_model = M("Business");
        $this->scanPay_model = D("ScanPay");
    }

    /**
     * 二维码规则 ID 使用authcode生成密文放置在二维码中
     * @DateTime 2017-07-25T10:53:19+0800
     * @return   [type]                   [description]
     */
    public function index()
    {
        
    }

    public function qrcode()
    {
        $id=$this->_userId;
        if (!($id>0) || $this->_userType!==2) {
            $this->ajaxReturn(0,array(),"ID错误");
        }
        $result=$this->business_model->where(['id'=>$id])->field("erweima")->find();
        if (!empty($result)) {
            $url=get_bbs_image($result['erweima'],"erweima");
            $this->ajaxReturn(1,['url'=>$url],"成功");
        }else{
            $this->ajaxReturn(0,array(),"二维码不存在!");
        }
    }

    public function scan()
    {
        $authcode=I("authcode");
        $id=sp_authcode($authcode);
        //$id=sp_authcode(urldecode($authcode));
        if (!is_numeric($id) || !($id>0) ) {
            $this->ajaxReturn(0,array(),"二维码错误");
        }

        $data=M('Business')->where(['id'=>$id])->field("id,name,logo")->find();
        $data['logo']=sp_get_image_url($data['logo']);
        

        if ($data!==false) {
            
            $this->ajaxReturn(1,$data,"成功！");
        }else{
            $this->ajaxReturn(0,M('Business')->getError(),"失败！");
        }
    }

    public function scanPay()
    {
        $authcode=I("authcode");
        $id=sp_authcode($authcode);
        if (!is_numeric($id) || !($id>0) ) {
            $this->ajaxReturn(0,array(),"二维码错误");
        }

        $data=I("post.");
        $data['user_type']=$this->_userType;
        $data['user_id']=$this->_userId;
        $data['receive_user_type']=2;
        $data['receive_user_id']=$id;
        $data['sn']=sp_get_order_sn();
        //$data['status']=0;

        $result=$this->scanPay_model->create($data);
        $result=$this->scanPay_model->add();
        $data['name']="扫一扫支付";

        if ($result!==false) {
            switch ($data['pay_type']) {
                case 1:
                    /*微信*/
                    $beforeOrder=(new WxpayService($this->_userType))->appPay($data['sn'], $data['name'], $data['total'],4);
                    break;
                case 2:
                    /*支付宝*/
                    $beforeOrder=(new AlipayService())->appPay($data['sn'], $data['name'], $data['total'],4);
                    break;
                    break;
                default:
                    break;
            }
            $this->ajaxReturn(1,$beforeOrder,"成功！");
        }else{
            $this->ajaxReturn(0,$this->scanPay_model->getError(),"失败！");
        }
    }

    public function test()
    {
        $id=sp_authencode(13);
        dump($id);
    }
    

}